package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父节点查询商品类目
     * @param pid
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<Category>> queryByParentId(@RequestParam(value = "pid", defaultValue = "0") Long pid) {
        return ResponseEntity.ok(categoryService.queryListByParent(pid));
    }

    /**
     * 查询所有一,二,三级分类信息
     * @return
     */
    @GetMapping("listAll")
    public ResponseEntity<List<Category>> queryAllCategory() {
        return ResponseEntity.ok(categoryService.queryAllCategory());
    }

    @RequestMapping("add")
    public void addCategory(Category category){
       this.categoryService.addCategory(category);
    }

    @RequestMapping("delete")
    public void deleteCategory(@RequestParam("id") Long id){
        this.categoryService.delete(id);
    }

    @RequestMapping("edit")
    public void editCategoryByParent(Category category){
        this.categoryService.editCategoryByParent(category);
    }

    @GetMapping("/bid/{bid}")
    public ResponseEntity<List<Category>> findByBrandId(@PathVariable("bid") Long bid) {
        List<Category> list = this.categoryService.findByBrandId(bid);
        if (list == null || list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }


    /**
     * 根据商品分类Ids查询分类
     * @param ids
     * @return
     */
    @GetMapping("list/ids")
    public ResponseEntity<List<Category>> queryCategoryByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(categoryService.queryCategoryByIds(ids));
    }
    @GetMapping("names")
    public ResponseEntity<List<String>> queryNameByIds(@RequestParam("ids") List<Long> ids){
        List<String > list = this.categoryService.queryNameByIds(ids);
        if (list == null || list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }
    /**
     * 根据3级分类id，查询1~3级的分类
     * @param id
     * @return
     */
    @GetMapping("all/level")
    public ResponseEntity<List<Category>> queryAllByCid3(@RequestParam("id") Long id){
        List<Category> list = this.categoryService.queryAllByCid3(id);
        if (list == null || list.size() < 1) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(list);
    }
    /**
     * 根据3级分类id，查询1~3级的分类
     * @param cid
     * @return
     */
    @GetMapping("all/{cid}")
    public ResponseEntity<List<Category>> queryCategoryListChildId(@PathVariable("cid")Long cid){
        if (null == cid){
            return ResponseEntity.badRequest().build();
        }
        List<Category> categories = categoryService.queryAllByCid3(cid);
        if (CollectionUtils.isEmpty(categories)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(categories);
    }
    @GetMapping("all")
    public ResponseEntity<List<Category>> queryAllCategoryList(){

        List<Category> categories = categoryService.queryAllCategory();
        if (CollectionUtils.isEmpty(categories)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(categories);
    }
}
